package com.quanyue.haircut.shop.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Principal;
import com.quanyue.auto.web.Scope;
import com.quanyue.haircut.domain.constant.UserRole;
import com.quanyue.haircut.domain.entity.IntegralProduct;
import com.quanyue.haircut.shop.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "积分商品")
@RestController
@RequestMapping("/api")
public class ProductController {
    @Autowired
    private ProductService productService;

    @ApiOperation(value = "创建商品")
    @Scope(role = UserRole.MANAGER)
    @PostMapping(value = "/integral/product", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <IntegralProduct> createProduct(@RequestBody IntegralProduct integralProduct,
                                                         @ApiIgnore Principal principal) {
        integralProduct.setMerchantNo(principal.getMerchantNo());
        return ServiceResult.success(productService.createProduct(integralProduct));
    }

    @ApiOperation(value = "修改商品")
    @Scope(role = UserRole.MANAGER)
    @PutMapping(value = "/integral/product", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> updateProduct(@RequestBody IntegralProduct integralProduct) {
        productService.updateProduct(integralProduct);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除商品")
    @Scope(role = UserRole.MANAGER)
    @DeleteMapping(value = "/integral/product", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> deleteProduct(@RequestParam Integer productId) {
        productService.deleteProduct(productId);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "商品列表")
    @GetMapping(value = "/integral/product", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <List <IntegralProduct>> productList(@ApiIgnore Principal principal) {
        return ServiceResult.success(productService.findByMerchantNo(principal.getMerchantNo()));
    }
}
